#include "sys.h"
#include "timer.h"
#include "norflash.h"
#include "umath.h"
#include "temp_controller.h"
#include "mcu_api.h"
#include "wifi_wb23.h"

#define PRESS_DOWN  1
#define PRESS_UP    2
#define PRESS_HOLD  3
#define PRESS_CLICK 4

u16 roomTemp;
s16 diffTemp;
u16 mode, fan_speed;
u16 wkq_switch = 0;
static u16 lastspeed = 0;
static u16 timing_switch = 0;
u16 timing_hour;
u16 timing_minute;
u16 timing_second;
u16 time_flag = 0;
u16 fan_speed_init = 0;
u16 mode_init = 0;
u16 cnt = 30;
u16 breathingScreen_cnt = 900;
u16 ScreenFlag; // 屏保状态机标志
u16 sleepFlag;  // 是否处于睡眠模式
Mock_Struct mock_var;
UpRtInfo upinfo;
u16 autoFlag123;
u16 comboAttackCnt; // 开发者模式连击计数
u16 productionTesting;

u16 tab[10][6] = {
    {0, 0, 0, 0, 0, 0}, // 空闲0
    {1, 0, 0, 0, 0, 0}, // 制冷1
    {0, 1, 0, 0, 0, 0}, // 制热2
    {0, 0, 1, 0, 0, 0}, // 除湿3
    {0, 0, 0, 1, 0, 0}, // 地暖4
    {0, 0, 0, 0, 1, 0}, // 吹风5
    {0, 1, 0, 1, 0, 0}, // 制热+地暖6
    {1, 0, 0, 0, 0, 1}, // 制冷+节能7
    {0, 1, 0, 0, 0, 1}, // 制热+节能8
    {0, 1, 0, 1, 0, 1}, // 制热+地暖+节能9
};

// 开发者参数
#define DEVELOPER_PARAMS_ADDR    0x0000
#define DEVELOPER_PARAMS_FLAG_OK 0x5AA5 // 更改此值可以初始化所有参数

struct DEVELOPER default_developer_params = {
    DEVELOPER_PARAMS_FLAG_OK, // 参数是否存在
    0,                        // wifi类型
    "V1.0.0",                 // 内机板软件版本
    "V1.0.0",                 // 温控器软件版本
    "DC0001",                 // 室内机型号
    2,                        // 室内机类型
    3000,                     // 制冷功耗
    4000,                     // 制热功耗
    1000,                     // 电机功耗
    2000,                     // 电加热功耗
    4000,                     // 地暖功耗
    1,                        // 设备号
    0};
struct DEVELOPER developer_params;

// 系统参数
#define SYS_PARAMS_ADDR    0x1000
#define SYS_PARAMS_FLAG_OK 0x5AA5 // 更改此值可以初始化所有参数

struct SYS default_sys_params = {
    SYS_PARAMS_FLAG_OK, // 参数是否存在
    1,                  // 模式  10种
    4,                  // 风速 低1 中2 高3 自动4
    1,                  // 定时开关
    12,                 // 设定时钟
    30,                 // 设定分钟
    25,                 // 设定温度  C 16~32  F 60-89
    1,                  // 温度单位  1C   2F
    16,                 // 温度下限
    32,                 // 温度上限
    0x6400,             // 亮度
    1,                  // 屏保时间
    1,                  // 屏保图
    3,                  // 主题
    0,                  // 语言
    1,                  // 息屏时间
    0,                  // 自动亮度
    0,                  // 睡眠模式开关
    22,                 // 开始时
    0,                  // 开始分
    6,                  // 结束时
    0,                  // 结束分
    1,                  // 按键音
    0x100               // 接近传感器阈值
};
struct SYS sys_params;
void UpData(u16 addr, u16 len);

// 加载开发者选项参数
void developer_params_load()
{
    Nor_Flash_read(DEVELOPER_PARAMS_ADDR, (u8*)&developer_params, sizeof(developer_params) / 2); // 读取系统参数
    if (developer_params.Flag != DEVELOPER_PARAMS_FLAG_OK)                                       // 存储器上没有该结构体的数据,说明仪器是第一次使用,需要复位一下参数
    {
        developer_params_reset(); // 复位系统参数
    }
}

// 复位开发者选项参数
void developer_params_reset()
{
    memcpy(&developer_params, &default_developer_params, sizeof(developer_params));
    developer_params_save(); // 保存参数
}

// 保存开发者选项参数
void developer_params_save()
{
    Nor_Flash_write(DEVELOPER_PARAMS_ADDR, (u8*)&developer_params, sizeof(developer_params) / 2);
}

// 加载系统参数
void sys_params_load()
{
    Nor_Flash_read(SYS_PARAMS_ADDR, (u8*)&sys_params, sizeof(sys_params) / 2); // 读取系统参数
    if (sys_params.Flag != SYS_PARAMS_FLAG_OK)                                 // 存储器上没有该结构体的数据,说明仪器是第一次使用,需要复位一下参数
    {
        sys_params_reset(); // 复位系统参数
    }
}

// 复位系统参数
void sys_params_reset()
{
    memcpy(&sys_params, &default_sys_params, sizeof(sys_params));
    sys_params_save(); // 保存参数
}

u16 sysParamsSaveFlag;
// 5s后保存系统参数
void sys_params_save()
{
    StartTimer(1, 5000);
    sysParamsSaveFlag = 1;
}

void sysParamsSave_Update(void)
{
    if (sysParamsSaveFlag == 1 && GetTimeOutFlag(1))
    {
        sysParamsSaveFlag = 0;
        Nor_Flash_write(SYS_PARAMS_ADDR, (u8*)&sys_params, sizeof(sys_params) / 2);
    }
}

void virtualTouch(u16 TouchType, u16 PressPosX, u16 PressPosY)
{
    u16 k_data[4];
    k_data[0] = 0x5aa5;
    k_data[1] = TouchType;
    k_data[2] = PressPosX;
    k_data[3] = PressPosY;
    write_dgus_vp(0xD4, (u8*)k_data, 4);
    do
    {
        delay_us(50);
        read_dgus_vp(0xD4, (u8*)k_data, 1);
    } while (k_data[0] != 0);
    delay_us(500);
}

// 修改增量调节的参数
void modifyInceaseControlPara(u8 page_id, u8 id, u16 minLimit, u16 step, u16 maxLimit)
{
    u8 tmp[20];

    tmp[0] = 0x5a;
    tmp[1] = 0xa5;
    tmp[2] = 0;
    tmp[3] = page_id;
    tmp[4] = id;
    tmp[5] = 2;
    tmp[6] = 0;
    tmp[7] = 2; // 读取
    write_dgus_vp(0x00B0, tmp, 4);
    do
    {
        delay_ms(2);
        read_dgus_vp(0x00B0, tmp, 1);
    } while (tmp[0] != 0);

    write_dgus_vp(0xb4 + 11, (u8*)&step, 1);     // 步长
    write_dgus_vp(0xb4 + 12, (u8*)&minLimit, 1); // 下限
    write_dgus_vp(0xb4 + 13, (u8*)&maxLimit, 1); // 上限

    tmp[0] = 0x5a;
    tmp[1] = 0xa5;
    tmp[2] = 0;
    tmp[3] = page_id;
    tmp[4] = id;
    tmp[5] = 2;
    tmp[6] = 0;
    tmp[7] = 3; // 写入
    write_dgus_vp(0x00B0, tmp, 4);
    do
    {
        delay_ms(2);
        read_dgus_vp(0x00B0, tmp, 1);
    } while (tmp[0] != 0);
}

void get_var_change_flag(void)
{
    u8 varChangeFlag[4];
    read_dgus_vp(0x0f00, varChangeFlag, 2); // D3=5A 表示有变量改变 D2:D1= 变量存储器指针 D0= 变量长度(字)
    if (varChangeFlag[0] == 0x5a)
    {
        varChangeFlag[0] = 0;
        write_dgus_vp(0x0f00, varChangeFlag, 1);
        switch ((u16)varChangeFlag[1] << 8 | varChangeFlag[2])
        {
            case 0x1070: // 语言
                read_dgus_vp(0x1070, (u8*)&sys_params.language, 1);
                sys_params_save();
                break;
            case 0x0082: // 亮度
                read_dgus_vp(0x0082, (u8*)&sys_params.brightness, 1);
                write_dgus_vp(0x2110, (u8*)&sys_params.brightness, 1);
                sys_params.autoLight = 0;
                write_dgus_vp(0x20D1, (u8*)&sys_params.autoLight, 1);
                sys_params_save();
                data_update(DPID_BACKLIGHT);
                break;
            case 0x1005: // 温度
                read_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                // if (sys_params.temp_unit == 2)
                // {
                //     sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                //     write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                // }
                if (wkq_switch == 1)
                {
                    if (sys_params.mode == 7)
                        sys_params.mode = 1;
                    else if (sys_params.mode == 8)
                        sys_params.mode = 2;
                    else if (sys_params.mode == 9)
                        sys_params.mode = 6;
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                }
                sys_params_save();
                data_update(DPID_TEMP_SET);
                break;
            case 0x2060: // 制冷按钮
                if (wkq_switch == 1)
                {
                    if (sys_params.mode == 8 || sys_params.mode == 9)
                    {
                        sys_params.mode = 7;
                        sys_params.set_temp = 26;
                        if (sys_params.temp_unit == 2)
                            sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                        write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                        sys_params.fan_speed = 1;
                        write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                    }
                    else if (sys_params.mode != 7)
                        sys_params.mode = 1;
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                data_update(DPID_TEMP_SET);
                break;
            case 0x2061: // 制热按钮
                if (wkq_switch == 1)
                {
                    if (sys_params.mode == 6 || sys_params.mode == 9)
                        sys_params.mode = 4;
                    else if (sys_params.mode == 7 || sys_params.mode == 8)
                        sys_params.mode = 8;
                    else if (sys_params.mode == 4)
                        sys_params.mode = 6;
                    else
                        sys_params.mode = 2;
                    if (sys_params.mode == 8 || sys_params.mode == 9)
                    {
                        sys_params.set_temp = 18;
                        if (sys_params.temp_unit == 2)
                            sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                        write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                        sys_params.fan_speed = 1;
                        write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                    }
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                data_update(DPID_TEMP_SET);
                break;
            case 0x2062: // 除湿按钮
                if (wkq_switch == 1)
                {
                    sys_params.mode = 3;
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                break;
            case 0x2063: // 地暖按钮
                if (wkq_switch == 1)
                {
                    if (sys_params.mode == 6)
                        sys_params.mode = 2;
                    else if (sys_params.mode == 2)
                        sys_params.mode = 6;
                    else if (sys_params.mode == 8)
                        sys_params.mode = 9;
                    else if (sys_params.mode == 9)
                        sys_params.mode = 8;
                    else
                        sys_params.mode = 4;
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                break;
            case 0x2064: // 吹风按钮
                if (wkq_switch == 1)
                {
                    sys_params.mode = 5;
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                break;
            case 0x2065: // 节能按钮
                if (wkq_switch == 1)
                {
                    if (sys_params.mode == 1)
                    {
                        sys_params.mode = 7;
                        sys_params.set_temp = 26;
                    }
                    else if (sys_params.mode == 2)
                    {
                        sys_params.mode = 8;
                        sys_params.set_temp = 18;
                    }
                    else if (sys_params.mode == 6)
                    {
                        sys_params.mode = 9;
                        sys_params.set_temp = 18;
                    }
                    else
                    {
                        break;
                    }
                    sys_params.fan_speed = 1;
                    if (sys_params.temp_unit == 2)
                        sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                    write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                    write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                    write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                    mode_update();
                    sys_params_save();
                }
                data_update(DPID_MODE);
                data_update(DPID_TEMP_SET);
                break;
            case 0x1028: // 模式开关
                read_dgus_vp(0x1028, (u8*)&wkq_switch, 1);
                switch_on_off();
                data_update(DPID_SWITCH);
                break;
            // case 0x1029: // 定时开关
            //     break;
            case 0x1330: // 风速
                if (wkq_switch == 1)
                {
                    read_dgus_vp(0x1330, (u8*)&sys_params.fan_speed, 1);
                    write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                    sys_params_save();
                }
                data_update(DPID_LEVEL);
                break;
            // case 0x1040: // 时钟
            //     read_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
            //     timing_hour = sys_params.set_time_hour;
            //     write_dgus_vp(0x1041, (u8*)&sys_params.set_time_hour, 1);
            //     sys_params_save();
            //     break;
            // case 0x1050: // 分钟
            //     read_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
            //     timing_minute = sys_params.set_time_minute;
            //     write_dgus_vp(0x1042, (u8*)&sys_params.set_time_minute, 1);
            //     sys_params_save();
            //     break;
            case 0x1060: // 温度单位
            {
                u16 temp_unit;
                read_dgus_vp(0x1060, (u8*)&temp_unit, 1);
                if (temp_unit != sys_params.temp_unit)
                {
                    sys_params.temp_unit = temp_unit;
                    if (sys_params.temp_unit == 2) // 摄氏度转华氏度
                    {
                        sys_params.set_temp = sys_params.set_temp * 1.8 + 32;
                        sys_params.Lower_temperature_limit = 60;
                        sys_params.Upper_temperature_limit = 89;
                    }
                    else if (sys_params.temp_unit == 1) // 华氏度转摄氏度
                    {
                        sys_params.set_temp = (sys_params.set_temp - 32) / 1.8;
                        sys_params.Lower_temperature_limit = 16;
                        sys_params.Upper_temperature_limit = 32;
                    }
                    write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1); // 设定温度
                    modifyInceaseControlPara(1, 4, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
                    modifyInceaseControlPara(1, 3, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
                }
                sys_params_save();
                data_update(DPID_TEMP_UNIT_CONVERT);
                data_update(DPID_TEMP_SET);
                break;
            }
            case 0x1080: // 屏保时间
                read_dgus_vp(0x1080, (u8*)&sys_params.screensaver_time, 1);
                sys_params_save();
                break;
            case 0x1081: // 息屏时间
                read_dgus_vp(0x1081, (u8*)&sys_params.breathingScreen_time, 1);
                sys_params_save();
                break;
            case 0x1090: // 屏保图
                read_dgus_vp(0x1090, (u8*)&sys_params.screensaver_var, 1);
                sys_params_save();
                break;
            case 0x10A0: // 主题风格
                read_dgus_vp(0x10A0, (u8*)&sys_params.theme, 1);
                sys_params_save();
                break;
            case 0x10B0: // 触摸屏保
                write_dgus_vp(0x0082, (u8*)&sys_params.brightness, 1);
                Page_Change(1);
                ScreenFlag = 0;
                break;
            case 0x2036:
                write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                write_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
                write_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
                break;

            case 0x2035:
                read_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                read_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
                read_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
                sys_params_save();

                if (sys_params.set_time_hour == 0 && sys_params.set_time_minute == 0 && timing_switch == 1)
                {
                    timing_switch = 0;
                    write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                }
                else
                {
                    if (timing_switch == 1)
                    {
                        StartTimer(2, 1000);
                        timing_hour = sys_params.set_time_hour;
                        timing_minute = sys_params.set_time_minute;
                        timing_second = 0;
                        write_dgus_vp(0x1041, (u8*)&timing_hour, 1);
                        write_dgus_vp(0x1042, (u8*)&timing_minute, 1);
                        write_dgus_vp(0x1043, (u8*)&timing_second, 1);

                        time_flag = 1;
                    }
                    else
                    {
                        KillTimer(2);
                        time_flag = 0;
                    }
                }
                data_update(DPID_COUNTDOWN_SET);
                data_update(DPID_COUNTDOWN_LEFT);
                break;
            case 0x0498:
                mcu_reset_wifi();
                break;
            case 0x20C0:
                if (developer_params.Wifi_type)
                {
                    Page_Change(23);
                }
                else
                {
                    Page_Change(19);
                }
                break;
            case 0x20C1:
                if (developer_params.Wifi_type)
                {
                    Page_Change(27);
                }
                else
                {
                    Page_Change(17);
                }
                break;
            case 0x8004:
                read_dgus_vp(0x8004, (u8*)&developer_params.DevID, 1);
                developer_params_save();
                break;

            case 0x9999:
                Page_Change(51);
                break;

            case 0x20d2: // 按键音
            {
                u8 cmd[4] = {0x5A, 0, 0, 0X32};
                read_dgus_vp(0x20d2, (u8*)&sys_params.keyTone, 1);
                if (sys_params.keyTone)
                {
                    // cmd[3] = 0x3A;
                    // write_dgus_vp(0x80, cmd, 2);
                    system_config(1, 2); // 开启按键音
                }
                else
                {
                    // cmd[3] = 0x32;
                    // write_dgus_vp(0x80, cmd, 2);
                    system_config(0, 2); // 关闭按键音
                }
            }
            break;
            case 0x20d3: // 定时设置
            {
                u16 tmp;
                read_dgus_vp(0x20d3, (u8*)&tmp, 1);
                switch (tmp)
                {
                    case 1:
                        sys_params.set_time_hour = 0;
                        sys_params.set_time_minute = 30;
                        break;
                    case 2:
                        sys_params.set_time_hour = 1;
                        sys_params.set_time_minute = 0;
                        break;
                    case 3:
                        sys_params.set_time_hour = 2;
                        sys_params.set_time_minute = 0;
                        break;
                    case 4:
                        sys_params.set_time_hour = 4;
                        sys_params.set_time_minute = 0;
                        break;
                    case 5:
                        sys_params.set_time_hour = 8;
                        sys_params.set_time_minute = 0;
                        break;
                    default:
                        break;
                }

                write_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
                write_dgus_vp(0x1041, (u8*)&sys_params.set_time_hour, 1);
                write_dgus_vp(0x1042, (u8*)&sys_params.set_time_minute, 1);
                write_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
                timing_hour = sys_params.set_time_hour;
                timing_minute = sys_params.set_time_minute;
                timing_second = 0;
                write_dgus_vp(0x1043, (u8*)&timing_second, 1);

                StartTimer(2, 1000);
                time_flag = 1;
                timing_switch = 1;
                write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                sys_params_save();
            }
            break;

            case 0x20d4: // 开关定时
            {
                u16 tmp;
                read_dgus_vp(0x20d3, (u8*)&tmp, 1);
                if (tmp == 0)
                {
                    virtualTouch(PRESS_CLICK, 0XFF00 + 0x01, 1); // 弹出菜单
                }
                else
                {
                    tmp = 0;
                    write_dgus_vp(0x20d3, (u8*)&tmp, 1);
                    time_flag = 0;
                    timing_switch = 0;
                    write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                    KillTimer(2);
                }
            }
            break;
            case 0x20D1: // 自动亮度
                read_dgus_vp(0x20D1, (u8*)&sys_params.autoLight, 1);
                sys_params_save();
                autoFlag123++;
                write_dgus_vp(0xB022, (u8*)&autoFlag123, 1);
                break;

            case 0x1036: // 睡眠模式
                read_dgus_vp(0x1036, (u8*)&sys_params.sleepModeSwitch, 1);
                sys_params_save();
                break;
            case 0x1037:
                read_dgus_vp(0x1037, (u8*)&sys_params.sleepModeStartHour, 1);
                sys_params_save();
                break;
            case 0x1038:
                read_dgus_vp(0x1038, (u8*)&sys_params.sleepModeStartMinute, 1);
                sys_params_save();
                break;
            case 0x1039:
                read_dgus_vp(0x1039, (u8*)&sys_params.sleepModeEndHour, 1);
                sys_params_save();
                break;
            case 0x103A:
                read_dgus_vp(0x103A, (u8*)&sys_params.sleepModeEndMinute, 1);
                sys_params_save();
                break;
            case 0x2100: // 产测
            {
                u16 tmp;
                read_dgus_vp(0x2100, (u8*)&tmp, 1);
                if (tmp == 1)
                {
                    Page_Change(51);
                    productionTesting = 1;
                    read_dgus_vp(0x20e4, (u8*)&tmp, 1);
                    if (tmp)
                    {
                        midWindSpeed = 1;
                        lowWindSpeed = 1;
                        highWindSpeed = 1;
                        valve1 = 1;
                        valve2 = 1;
                        linkage = 1;
                    }
                    else
                    {
                        midWindSpeed = 0;
                        lowWindSpeed = 0;
                        highWindSpeed = 0;
                        valve1 = 0;
                        valve2 = 0;
                        linkage = 0;
                    }
                }
                else
                {
                    productionTesting = 0;
                }
                break;
            }

            case 0x20e4: // 继电器测试
            {
                u16 tmp;
                read_dgus_vp(0x20e4, (u8*)&tmp, 1);
                if (tmp)
                {
                    midWindSpeed = 1;
                    lowWindSpeed = 1;
                    highWindSpeed = 1;
                    valve1 = 1;
                    valve2 = 1;
                    linkage = 1;
                }
                else
                {
                    midWindSpeed = 0;
                    lowWindSpeed = 0;
                    highWindSpeed = 0;
                    valve1 = 0;
                    valve2 = 0;
                    linkage = 0;
                }
            }
            break;
            case 0x2101:
                StartTimer(6, 1000);
                if (++comboAttackCnt >= 5)
                {
                    u16 tmp = 1;
                    write_dgus_vp(0x2104, (u8*)&tmp, 1);
                    KillTimer(6);
                }
                break;
            case 0x2102:
                comboAttackCnt = 0;
                write_dgus_vp(0x2104, (u8*)&comboAttackCnt, 1);
                break;
            case 0x2103:
            {
                u16 key;
                if (comboAttackCnt >= 5)
                {
                    read_dgus_vp(0x2103, (u8*)&key, 1);
                    virtualTouch(PRESS_CLICK, 0XFF00 + key, 1);
                    key = 0;
                    write_dgus_vp(0x2103, (u8*)&key, 1);
                }
            }
            break;
            case 0x1100:
            {
                read_dgus_vp(0x1100, developer_params.Internal_board_software_version, 8);
                developer_params_save();
            }
            break;
            case 0x1110:
            {
                read_dgus_vp(0x1110, developer_params.Indoor_unit_model, 8);
                developer_params_save();
            }
            break;
            case 0x1118:
            {
                read_dgus_vp(0x1118, (u8*)&developer_params.Indoor_unit_type, 1);
                developer_params_save();
            }
            break;
            case 0x1119:
            {
                read_dgus_vp(0x1119, (u8*)&developer_params.Cooling_power_consumption, 1);
                developer_params_save();
            }
            break;
            case 0x111A:
            {
                read_dgus_vp(0x111A, (u8*)&developer_params.Heating_power_consumption, 1);
                developer_params_save();
            }
            break;
            case 0x111B:
            {
                read_dgus_vp(0x111B, (u8*)&developer_params.Motor_power_consumption, 1);
                developer_params_save();
            }
            break;
            case 0x111C:
            {
                read_dgus_vp(0x111C, (u8*)&developer_params.Electric_heating_power_consumption, 1);
                developer_params_save();
            }
            break;
            case 0x111D:
            {
                read_dgus_vp(0x111D, (u8*)&developer_params.Floor_heating_power_consumption, 1);
                developer_params_save();
            }
            break;
            case 0xB021:
            {
                u16 tmp;
                read_dgus_vp(0xB011, (u8*)&tmp, 1);
                if (tmp >= 0xFFF)
                    sys_params.VCNL4200Threshold = 0xFFF;
                else
                    sys_params.VCNL4200Threshold = tmp + 0x30;
                write_dgus_vp(0xB020, (u8*)&sys_params.VCNL4200Threshold, 1);
                sys_params_save();
                autoFlag123++;
                write_dgus_vp(0xB022, (u8*)&autoFlag123, 1);
                break;
            }
            case 0xB020:
                read_dgus_vp(0xB020, (u8*)&sys_params.VCNL4200Threshold, 1);
                sys_params_save();
                autoFlag123++;
                write_dgus_vp(0xB022, (u8*)&autoFlag123, 1);
                break;
            default: break;
        }
    }
}

// 系统参数初始化
void sys_params_init()
{
    // sys_params.brightness = (100 << 8);
    if (sys_params.autoLight == 0)
        write_dgus_vp(0x0082, (u8*)&sys_params.brightness, 1);
    write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
    write_dgus_vp(0x1020, (u8*)tab[mode_init], 6);
    write_dgus_vp(0x1028, (u8*)&wkq_switch, 1);
    write_dgus_vp(0x1030, (u8*)&fan_speed_init, 1);
    write_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
    write_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
    timing_hour = sys_params.set_time_hour;
    timing_minute = sys_params.set_time_minute;
    write_dgus_vp(0x1041, (u8*)&sys_params.set_time_hour, 1);
    write_dgus_vp(0x1042, (u8*)&sys_params.set_time_minute, 1);
    write_dgus_vp(0x1060, (u8*)&sys_params.temp_unit, 1);
    write_dgus_vp(0x1070, (u8*)&sys_params.language, 1);
    write_dgus_vp(0x1080, (u8*)&sys_params.screensaver_time, 1);
    write_dgus_vp(0x1090, (u8*)&sys_params.screensaver_var, 1);
    write_dgus_vp(0x10A0, (u8*)&sys_params.theme, 1);
    write_dgus_vp(0x20D1, (u8*)&sys_params.autoLight, 1);

    write_dgus_vp(0x20d2, (u8*)&sys_params.keyTone, 1);
    if (sys_params.keyTone)
    {
        // u8 cmd[4] = {0x5A, 0, 0, 0X3A};
        // write_dgus_vp(0x80, cmd, 2);
        system_config(1, 2); // 开启按键音
    }
    else
    {
        // u8 cmd[4] = {0x5A, 0, 0, 0X32};
        // write_dgus_vp(0x80, cmd, 2);
        system_config(0, 2); // 关闭按键音
    }

    write_dgus_vp(0x1036, (u8*)&sys_params.sleepModeSwitch, 1);
    write_dgus_vp(0x1037, (u8*)&sys_params.sleepModeStartHour, 1);
    write_dgus_vp(0x1038, (u8*)&sys_params.sleepModeStartMinute, 1);
    write_dgus_vp(0x1039, (u8*)&sys_params.sleepModeEndHour, 1);
    write_dgus_vp(0x103A, (u8*)&sys_params.sleepModeEndMinute, 1);
    write_dgus_vp(0x20D3, (u8*)&timing_switch, 1);
    write_dgus_vp(0x1081, (u8*)&sys_params.breathingScreen_time, 1);
    write_dgus_vp(0xB020, (u8*)&sys_params.VCNL4200Threshold, 1);
    SetPinOut(1, 1);
    SetPinOut(1, 2);
    SetPinOut(1, 3);
    SetPinOut(1, 4);
    SetPinOut(1, 5);
    SetPinOut(1, 6);
    valve1 = 0;
    valve2 = 0;
    linkage = 0;
    lowWindSpeed = 0;
    midWindSpeed = 0;
    highWindSpeed = 0;
    modifyInceaseControlPara(1, 4, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
    modifyInceaseControlPara(1, 3, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
}

// 开发者选项参数初始化
void developer_params_init()
{
    write_dgus_vp(0x1100, (u8*)&developer_params.Internal_board_software_version[0], 30);
    write_dgus_vp(0x8004, (u8*)&developer_params.DevID, 1);
}

void get_roomTemp(void)
{
    read_dgus_vp(0x3e, (u8*)&roomTemp, 1);
    roomTemp += 10;
    if (sys_params.temp_unit == 2)
    {
        roomTemp = roomTemp * 1.8 + 320;
    }
    if (roomTemp % 10 <= 2)
        roomTemp -= 10;

    roomTemp /= 10;
    write_dgus_vp(0x1010, (u8*)&roomTemp, 1);
}

void TimingFunc_Update(void)
{
    if (timing_switch && GetTimeOutFlag(2))
    {
        StartTimer(2, 1000);
        if (timing_second)
        {
            timing_second--;
        }
        else
        {
            if (timing_minute)
            {

                timing_minute--;
                timing_second = 59;
                data_update(DPID_COUNTDOWN_LEFT);
            }
            else
            {
                if (timing_hour == 0)
                {

                    data_update(DPID_COUNTDOWN_LEFT);
                    wkq_switch = !wkq_switch;
                    write_dgus_vp(0x1028, (u8*)&wkq_switch, 1);
                    switch_on_off();
                    timing_switch = 0;
                    write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                    time_flag = 0;
                }
                else
                {
                    timing_hour--;
                    timing_minute = 59;
                    timing_second = 59;
                }
            }
        }
        write_dgus_vp(0x1041, (u8*)&timing_hour, 1);
        write_dgus_vp(0x1042, (u8*)&timing_minute, 1);
        write_dgus_vp(0x1043, (u8*)&timing_second, 1);
    }
}

void devCtr(void)
{
    if (productionTesting)
        return;

    if (lastspeed != sys_params.fan_speed)
    {
        lastspeed = sys_params.fan_speed;
        if (sys_params.fan_speed == 1)
        {
            midWindSpeed = 0;
            highWindSpeed = 0;
            lowWindSpeed = 1;
        }
        else if (sys_params.fan_speed == 2)
        {
            highWindSpeed = 0;
            lowWindSpeed = 0;
            midWindSpeed = 1;
        }
        else if (sys_params.fan_speed == 3)
        {
            midWindSpeed = 0;
            lowWindSpeed = 0;
            highWindSpeed = 1;
        }
    }
    if (sys_params.fan_speed == 4 && wkq_switch == 1)
    {
        diffTemp = (s16)sys_params.set_temp - roomTemp;

        if ((diffTemp <= 1 && diffTemp >= 0) || (diffTemp <= 0 && diffTemp >= -1))
        {
            midWindSpeed = 0;
            highWindSpeed = 0;
            lowWindSpeed = 1;
        }
        else if ((diffTemp <= 2 && diffTemp > 1) || (diffTemp < -1 && diffTemp >= -2))
        {
            highWindSpeed = 0;
            lowWindSpeed = 0;
            midWindSpeed = 1;
        }
        else if (diffTemp > 2 || diffTemp < -2)
        {
            midWindSpeed = 0;
            lowWindSpeed = 0;
            highWindSpeed = 1;
        }
    }
}

// 模式开关机后的动作
void switch_on_off()
{
    if (productionTesting)
        return;

    if (wkq_switch == 1) // 开机
    {
        mode = sys_params.mode;
        write_dgus_vp(0x1020, (u8*)tab[mode], 6);
        mode_update();
        lastspeed = 0;
        devCtr();
        fan_speed = sys_params.fan_speed;
        write_dgus_vp(0x1030, (u8*)&fan_speed, 1);
        linkage = 1;
    }
    else if (wkq_switch == 0) // 关机
    {
        mode = 0;
        write_dgus_vp(0x1020, (u8*)tab[mode], 6);
        mode_update();
        fan_speed = 0;
        write_dgus_vp(0x1030, (u8*)&fan_speed, 1);
        lowWindSpeed = 0;
        midWindSpeed = 0;
        highWindSpeed = 0;
        linkage = 0;
    }
}

void mode_update()
{
    if (wkq_switch == 1)
    {
        mode = sys_params.mode;
    }
    else
    {
        mode = 0;
    }

    switch (mode)
    {
        case 0: // 空闲
            valve1 = 0;
            valve2 = 0;
            break;
        case 1: // 制冷
            valve1 = 1;
            valve2 = 0;
            break;
        case 2: // 制热
            valve1 = 1;
            valve2 = 0;
            break;
        case 3: // 除湿
            valve1 = 0;
            valve2 = 0;
            break;
        case 4: // 地暖
            valve1 = 0;
            valve2 = 1;
            break;
        case 5: // 吹风
            valve1 = 0;
            valve2 = 0;
            break;
        case 6: // 制热+地暖
            valve1 = 1;
            valve2 = 1;
            break;
        case 7: // 制冷+节能
            valve1 = 1;
            valve2 = 0;
            break;
        case 8: // 制热+节能
            valve1 = 1;
            valve2 = 0;
            break;
        case 9: // 制热+节能+地暖
            valve1 = 1;
            valve2 = 1;
            break;
    }
}

// 屏保动作
void screensaver()
{
    u16 brightness;
    u8 my_page_temp[2] = {0}; // 存放页面
    read_dgus_vp(PIC_NOW, my_page_temp, 2);

    if (my_page_temp[1] >= 51)
        return;

    if (!GetTimeOutFlag(3))
        return;
    StartTimer(3, 1000);

    if (GetTouchStatus() != 0) // 设定时间内触摸
        ScreenFlag = 0;

    switch (ScreenFlag)
    {
        case 0: // 赋初值
            switch (sys_params.screensaver_time)
            {
                case 1: // 30s
                    cnt = 30;
                    break;
                case 2: // 60s
                    cnt = 60;
                    break;
                case 3: // 90s
                    cnt = 90;
                    break;
            }

            switch (sys_params.breathingScreen_time)
            {
                case 0: // 常亮
                    break;
                case 1: // 15min
                    breathingScreen_cnt = 900;
                    break;
                case 2: // 30min
                    breathingScreen_cnt = 1800;
                    break;
                case 3: // 60min
                    breathingScreen_cnt = 3600;
                    break;
            }

            if (sys_params.sleepModeSwitch == 1 && sleepFlag == 1)
            {
                cnt = 30; // 休眠模式30s息屏
            }

            ScreenFlag = 1;
            break;
        case 1: // 等待屏保
            cnt--;
            if (sys_params.breathingScreen_time)
                breathingScreen_cnt--;

            if (cnt == 0) // 到了设定时间未触摸
            {

                if (wkq_switch == 1)
                {
                    brightness = 0x0A00;
                    ScreenFlag = 2;
                }
                else
                {
                    brightness = 0x0000;
                    ScreenFlag = 3;
                }

                write_dgus_vp(0x0082, (u8*)&brightness, 1);
                read_dgus_vp(0x1090, (u8*)&sys_params.screensaver_var, 1);
                // GetPageID();
                Page_Change(sys_params.screensaver_var + 30);
            }
            break;

        case 2: // 等待息屏

            if (sys_params.sleepModeSwitch == 1 && sleepFlag == 1)
            {
                brightness = 0x0000;
                write_dgus_vp(0x0082, (u8*)&brightness, 1);
                Page_Change(sys_params.screensaver_var + 30);
                ScreenFlag = 3;
            }

            if (sys_params.breathingScreen_time)
            {
                breathingScreen_cnt--;
                if (breathingScreen_cnt == 0)
                {
                    brightness = 0x0000;
                    write_dgus_vp(0x0082, (u8*)&brightness, 1);
                    Page_Change(sys_params.screensaver_var + 30);
                    ScreenFlag = 3;
                }
            }
            else
                ScreenFlag = 3;
            break;
        default:
            break;
    }
}

int isTimeInSleepMode(int currentHour, int currentMinute)
{
    if (sys_params.sleepModeStartHour < sys_params.sleepModeEndHour ||
        (sys_params.sleepModeStartHour == sys_params.sleepModeEndHour &&
         sys_params.sleepModeStartMinute <= sys_params.sleepModeEndMinute))
    {
        // 睡眠模式时间范围未跨越午夜
        if ((currentHour > sys_params.sleepModeStartHour ||
             (currentHour == sys_params.sleepModeStartHour &&
              currentMinute >= sys_params.sleepModeStartMinute)) &&
            (currentHour < sys_params.sleepModeEndHour ||
             (currentHour == sys_params.sleepModeEndHour &&
              currentMinute <= sys_params.sleepModeEndMinute)))
        {
            return 1;
        }
    }
    else
    {
        // 睡眠模式时间范围跨越午夜
        if ((currentHour > sys_params.sleepModeStartHour ||
             (currentHour == sys_params.sleepModeStartHour &&
              currentMinute >= sys_params.sleepModeStartMinute)) ||
            (currentHour < sys_params.sleepModeEndHour ||
             (currentHour == sys_params.sleepModeEndHour &&
              currentMinute <= sys_params.sleepModeEndMinute)))
        {
            return 1; // 当前时间在睡眠模式范围内
        }
    }
    return 0; // 当前时间不在睡眠模式范围内
}

void sleepMode_updata()
{
    u8 time[2];
    u16 tmpFlag;
    static u16 lastSleepModeSwitch; // 切换睡眠开关时保证更新一次状态

    if (sys_params.sleepModeSwitch == 0) return;

    read_dgus_vp(0x12, time, 1);

    tmpFlag = isTimeInSleepMode(time[0], time[1]);

    if (tmpFlag != sleepFlag || lastSleepModeSwitch != sys_params.sleepModeSwitch)
    {
        lastSleepModeSwitch = sys_params.sleepModeSwitch;
        sleepFlag = tmpFlag;

        if (tmpFlag == 1)
        {
            // 静音
            // u8 cmd[4] = {0x5A, 0, 0, 0X32};
            // write_dgus_vp(0x80, cmd, 2);
            system_config(0, 2); // 关闭按键音
            if (cnt > 30)
            {
                cnt = 30; // 固定30s屏保
            }
        }
        else if (tmpFlag == 0 && sys_params.keyTone == 1)
        {
            // 打开按键音
            // u8 cmd[4] = {0x5A, 0, 0, 0X3A};
            // write_dgus_vp(0x80, cmd, 2);
            system_config(1, 2); // 开启按键音
        }
    }
}

void UpInfo_Chk(u8 cmd)
{
    extern u8 Touchstate;
    static UpRtInfo old; // 旧信息
    UpRtInfo down;       // 当前信息
    if (cmd == UPINFO_FLUSH)
    {
        // 更新信息到Upinfo
        UpInfo_Update();
        read_dgus_vp(UP_ADDR_BASE, (u8*)&down, sizeof(UpRtInfo) / 2);
        // 上传信息检测
        if (old.WorkMode != upinfo.WorkMode)
        {
            write_dgus_vp(0xA000, (u8*)&upinfo.WorkMode, 1);
            UpData(0xA000, 1);
        }
        if (old.Backlight != upinfo.Backlight)
        {
            if (!Touchstate)
            {
                write_dgus_vp(0xA009, (u8*)&upinfo.Backlight, 1);
                UpData(0xA009, 1);
            }
        }
        if (old.ECOMode != upinfo.ECOMode)
        {
            write_dgus_vp(0xA001, (u8*)&upinfo.ECOMode, 1);
            UpData(0xA001, 1);
        }
        if (old.CountDown != upinfo.CountDown)
        {
            write_dgus_vp(0xA003, (u8*)&upinfo.CountDown, 1);
            UpData(0xA003, 1);
        }
        if (old.TempCur != upinfo.TempCur)
        {
            write_dgus_vp(0xA004, (u8*)&upinfo.TempCur, 1);
            UpData(0xA004, 1);
        }
        if (old.CountDownSet != upinfo.CountDownSet)
        {
            write_dgus_vp(0xA002, (u8*)&upinfo.CountDownSet, 1);
            UpData(0xA002, 1);
        }
        if (old.Switch != upinfo.Switch)
        {
            write_dgus_vp(0xA006, (u8*)&upinfo.Switch, 1);
            UpData(0xA006, 1);
        }
        if (old.TempSet != upinfo.TempSet)
        {
            write_dgus_vp(0xA005, (u8*)&upinfo.TempSet, 1);
            UpData(0xA005, 1);
        }
        if (old.TempUint != upinfo.TempUint)
        {
            write_dgus_vp(0xA007, (u8*)&upinfo.TempUint, 1);
            UpData(0xA007, 1);
        }
        if (old.WindLevel != upinfo.WindLevel)
        {
            write_dgus_vp(0xA008, (u8*)&upinfo.WindLevel, 1);
            UpData(0xA008, 1);
        }
        // 下发信息检测
        if (down.WorkMode != old.WorkMode || down.ECOMode != old.ECOMode)
        {
            if (down.ECOMode != old.ECOMode)
            {
                if ((down.ECOMode == 1 && sys_params.mode < 7) || (down.ECOMode == 0 && sys_params.mode >= 7))
                {
                    mock_var.touch = 0x2065;
                    mock_varchg(&mock_var);
                }
            }
            if (down.WorkMode != old.WorkMode)
            {

                switch (down.WorkMode)
                {
                    case 0:
                        mock_var.touch = 0x2060;
                        break;
                    case 1:
                        mock_var.touch = 0x2061;
                        break;
                    case 2:
                        mock_var.touch = 0x2062;
                        break;
                    case 3:
                        mock_var.touch = 0x2063;
                        break;
                    case 4:
                        mock_var.touch = 0x2064;
                        break;
                    case 5:
                        mock_var.touch = 0x2066;
                        break;
                    default:
                        break;
                }
                mock_varchg(&mock_var);
            }
            if (mode == 0)
            {
                upinfo.WorkMode = 0;
                upinfo.ECOMode = 0;
            }
            else if (1 <= mode && mode <= 6)
            {
                upinfo.WorkMode = mode - 1;
                upinfo.ECOMode = 0;
            }
            else if (mode == 7)
            {
                upinfo.WorkMode = 0;
                upinfo.ECOMode = 1;
            }
            else if (mode == 8)
            {
                upinfo.WorkMode = 1;
                upinfo.ECOMode = 1;
            }
            else if (mode == 9)
            {
                upinfo.WorkMode = 5;
                upinfo.ECOMode = 1;
            }
            write_dgus_vp(0xA000, (u8*)&upinfo.WorkMode, 2);
            // write_dgus_vp(0xA001, (u8 *)&upinfo.ECOMode, 1);
            UpData(0xA001, 1);
            UpData(0xA000, 1);
        }
        if (down.Backlight != old.Backlight)
        {
            if (!Touchstate)
            {
                mock_var.touch = 0x82;
                mock_var.backlight = down.Backlight;
                mock_varchg(&mock_var);
                upinfo.Backlight = (sys_params.brightness >> 8);
                write_dgus_vp(0xA009, (u8*)&upinfo.Backlight, 1);
                UpData(0xA009, 1);
            }
        }

        if (down.CountDownSet != old.CountDownSet)
        {
            mock_var.countdown_set = down.CountDownSet;
            mock_var.touch = 0x1029;
            mock_varchg(&mock_var);
            upinfo.CountDownSet = sys_params.set_time_hour;
            write_dgus_vp(0xA002, (u8*)&upinfo.CountDownSet, 1);
            UpData(0xA002, 1);
        }
        if (down.Switch != old.Switch)
        {
            mock_var.touch = 0x1028;
            mock_var.wkq_switch = down.Switch;
            mock_varchg(&mock_var);
            upinfo.Switch = wkq_switch;
            write_dgus_vp(0xA006, (u8*)&upinfo.Switch, 1);
            UpData(0xA006, 1);
        }
        if (down.TempSet != old.TempSet)
        {
            mock_var.temp = (u16)down.TempSet;
            mock_var.touch = 0x1005;
            mock_varchg(&mock_var);
            upinfo.TempSet = sys_params.set_temp;
            write_dgus_vp(0xA005, (u8*)&upinfo.TempSet, 1);
            UpData(0xA005, 1);
        }
        if (down.TempUint != old.TempUint)
        {
            mock_var.temp_unit = down.TempUint;
            mock_var.touch = 0x1060;
            mock_varchg(&mock_var);
            upinfo.TempUint = sys_params.temp_unit;
            write_dgus_vp(0xA007, (u8*)&upinfo.TempUint, 1);
            UpData(0xA007, 1);
        }
        if (down.WindLevel != old.WindLevel)
        {
            mock_var.fan_speed = down.WindLevel;
            mock_var.touch = 0x1330;
            mock_varchg(&mock_var);
            upinfo.WindLevel = sys_params.fan_speed;
            write_dgus_vp(0xA008, (u8*)&upinfo.WindLevel, 1);
            UpData(0xA008, 1);
        }

        // write_dgus_vp(UP_ADDR_BASE, (u8 *)&upinfo, sizeof(UpRtInfo) / 2);
        // 更新信息到Upinfo
        if (!Touchstate)
        {
            old = upinfo;
        }
        else
        {
            u16 backlight = old.Backlight;
            old = upinfo;
            old.Backlight = backlight;
        }
    }
    else if (cmd == UPINFO_INIT)
    {
        UpInfo_Update();
        write_dgus_vp(UP_ADDR_BASE, (u8*)&upinfo, sizeof(UpRtInfo) / 2);
        old = upinfo;
    }
}
void UpInfo_Update(void)
{

    upinfo.Backlight = (sys_params.brightness >> 8);
    write_dgus_vp(0x2111, (u8*)&sys_params.brightness, 1);
    upinfo.TempSet = sys_params.set_temp;
    upinfo.WindLevel = sys_params.fan_speed;
    upinfo.Switch = wkq_switch;
    if (roomTemp)
    {
        upinfo.TempCur = roomTemp;
    }
    else
    {
        upinfo.TempCur = 26;
    }
    upinfo.CountDownSet = sys_params.set_time_hour;
    upinfo.TempUint = sys_params.temp_unit;
    upinfo.CountDown = timing_hour * 60 + timing_minute;
    if (mode == 0)
    {
        upinfo.WorkMode = 0;
        upinfo.ECOMode = 0;
    }
    else if (1 <= mode && mode <= 6)
    {
        upinfo.WorkMode = mode - 1;
        upinfo.ECOMode = 0;
    }
    else if (mode == 7)
    {
        upinfo.WorkMode = 0;
        upinfo.ECOMode = 1;
    }
    else if (mode == 8)
    {
        upinfo.WorkMode = 1;
        upinfo.ECOMode = 1;
    }
    else if (mode == 9)
    {
        upinfo.WorkMode = 5;
        upinfo.ECOMode = 1;
    }
}
void mock_varchg(Mock_Struct* mock)
{
    switch (mock->touch)
    {
        case 0x0082: // 亮度
        {
            sys_params.brightness = (mock->backlight << 8);
            if (cnt != 0)
            {
                write_dgus_vp(0x0082, (u8*)&sys_params.brightness, 1);
            }
            sys_params_save();
        }

        break;
        case 0x1005: // 温度
            sys_params.set_temp = mock->temp;
            write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
            if (wkq_switch == 1)
            {
                if (sys_params.mode == 7)
                    sys_params.mode = 1;
                else if (sys_params.mode == 8)
                    sys_params.mode = 2;
                else if (sys_params.mode == 9)
                    sys_params.mode = 6;
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
            }
            sys_params_save();
            break;

        case 0x2060: // 制冷按钮
            if (wkq_switch == 1)
            {
                if (sys_params.mode == 8 || sys_params.mode == 9)
                {
                    sys_params.mode = 7;
                    sys_params.set_temp = 26;
                    if (sys_params.temp_unit == 2)
                        sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                    write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                    sys_params.fan_speed = 1;
                    write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                }
                else if (sys_params.mode != 7)
                    sys_params.mode = 1;
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2061: // 制热按钮
            if (wkq_switch == 1)
            {
                if (sys_params.mode >= 7)
                {
                    sys_params.mode = 8; // 制热节能
                }
                else
                {
                    sys_params.mode = 2; // 制热
                }
                if (sys_params.mode == 8)
                {
                    sys_params.set_temp = 18;
                    if (sys_params.temp_unit == 2)
                        sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                    write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                    sys_params.fan_speed = 1;
                    write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                }
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2062: // 除湿按钮
            if (wkq_switch == 1)
            {
                sys_params.mode = 3;
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2063: // 地暖按钮
            if (wkq_switch == 1)
            {
                sys_params.mode = 4;
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2064: // 吹风按钮
            if (wkq_switch == 1)
            {
                sys_params.mode = 5;
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2065: // 节能按钮
            if (wkq_switch == 1)
            {
                if (sys_params.mode == 1)
                {
                    sys_params.mode = 7;
                    sys_params.set_temp = 26;
                }
                else if (sys_params.mode == 2)
                {
                    sys_params.mode = 8;
                    sys_params.set_temp = 18;
                }
                else if (sys_params.mode == 6)
                {
                    sys_params.mode = 9;
                    sys_params.set_temp = 18;
                }
                else
                {
                    break;
                }
                sys_params.fan_speed = 1;
                if (sys_params.temp_unit == 2)
                    sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x2066: // 制热+地暖
            if (wkq_switch == 1)
            {
                if (sys_params.mode >= 8)
                {
                    sys_params.mode = 9;
                    sys_params.set_temp = 18;
                    sys_params.fan_speed = 1;
                    if (sys_params.temp_unit == 2)
                        sys_params.set_temp = sys_params.set_temp * 1.8f + 32;
                }
                else
                {
                    sys_params.mode = 6;
                }
                write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1);
                write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                write_dgus_vp(0x1020, (u8*)tab[sys_params.mode], 6);
                mode_update();
                sys_params_save();
            }
            break;
        case 0x1028: // 模式开关

            wkq_switch = mock_var.wkq_switch;

            write_dgus_vp(0x0082, (u8*)&sys_params.brightness, 1);
            Page_Change(1);
            ScreenFlag = 0;

            write_dgus_vp(0x1028, (u8*)&wkq_switch, 1);
            switch_on_off();
            break;
        case 0x1330: // 风速
            if (wkq_switch == 1)
            {
                sys_params.fan_speed = mock->fan_speed;
                write_dgus_vp(0x1330, (u8*)&sys_params.fan_speed, 1);
                write_dgus_vp(0x1030, (u8*)&sys_params.fan_speed, 1);
                sys_params_save();
            }
            break;
        case 0x1040: // 时钟
            read_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
            timing_hour = sys_params.set_time_hour;
            write_dgus_vp(0x1041, (u8*)&sys_params.set_time_hour, 1);
            sys_params_save();
            break;
        case 0x1050: // 分钟
            read_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
            timing_minute = sys_params.set_time_minute;
            write_dgus_vp(0x1042, (u8*)&sys_params.set_time_minute, 1);
            sys_params_save();
            break;
        case 0x1060: // 温度单位
        {
            if (mock->temp_unit != sys_params.temp_unit)
            {
                write_dgus_vp(0x1060, (u8*)&mock->temp_unit, 1);
                sys_params.temp_unit = mock->temp_unit;
                if (sys_params.temp_unit == 2) // 摄氏度转华氏度
                {
                    sys_params.set_temp = sys_params.set_temp * 1.8 + 32;
                    sys_params.Lower_temperature_limit = 60;
                    sys_params.Upper_temperature_limit = 89;
                }
                else if (sys_params.temp_unit == 1) // 华氏度转摄氏度
                {
                    sys_params.set_temp = (sys_params.set_temp - 32) / 1.8;
                    sys_params.Lower_temperature_limit = 16;
                    sys_params.Upper_temperature_limit = 32;
                }
                write_dgus_vp(0x1005, (u8*)&sys_params.set_temp, 1); // 设定温度
                modifyInceaseControlPara(1, 4, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
                modifyInceaseControlPara(1, 3, sys_params.Lower_temperature_limit, 1, sys_params.Upper_temperature_limit);
            }
            sys_params_save();
            break;
        }
        case 0x1029: // 定时开关
            if (mock->countdown_set)
            {
                sys_params.set_time_hour = mock->countdown_set;
                sys_params.set_time_minute = 0;
                timing_hour = sys_params.set_time_hour;
                timing_minute = sys_params.set_time_minute;
                write_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
                write_dgus_vp(0x1041, (u8*)&sys_params.set_time_hour, 1);
                write_dgus_vp(0x1042, (u8*)&sys_params.set_time_minute, 1);
                write_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
                StartTimer(2, 1000);
                time_flag = 1;
                timing_switch = 1;
                write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                sys_params_save();
            }
            else
            {
                time_flag = 0;
                timing_switch = 0;
                sys_params.set_time_hour = 0;
                sys_params.set_time_minute = 0;
                timing_hour = sys_params.set_time_hour;
                timing_minute = sys_params.set_time_minute;
                write_dgus_vp(0x1040, (u8*)&sys_params.set_time_hour, 1);
                write_dgus_vp(0x1050, (u8*)&sys_params.set_time_minute, 1);
                write_dgus_vp(0x1029, (u8*)&timing_switch, 1);
                KillTimer(2);
            }

            break;
    }
}
void UpData(u16 addr, u16 len)
{
    u8 cmd[6];
    cmd[0] = 0x5A;
    cmd[1] = 0xA5;
    cmd[2] = (u8)(addr >> 8);
    cmd[3] = (u8)(addr);
    cmd[4] = (u8)(len >> 8);
    cmd[5] = (u8)(len);
    write_dgus_vp(0x401, cmd, 3);
    // do
    // {
    //     delay_us(100);
    //     read_dgus_vp(0x401, cmd, 1);

    // } while (cmd[0]);
}
